// ie placeholder
if (!+"\v1" || (document.documentMode && document.documentMode === 9)) {
    $('#search').val('请输入用户名搜索')
}

//初始获取此用户关系
$.ajax({
    url: "tree.json",
    type: 'post',
    dataType: 'json',
    data: {},
    success: function (data) {
        if (data.code === 1) {
            layui.use('tree', function () {
                layui.tree({
                    elem: '#demo' //传入元素选择器
                    , skin: 'coral' //设定颜色
                    , nodes: data.tree //获取数据
                });
            });
        }
    }
});

//搜索某用户关系
$('.searchBtn').on('touchend click', function (e) {
    if ($('#search').val()) {
        e.preventDefault();
        $.ajax({
            url: "tree.json",
            type: 'post',
            dataType: 'json',
            data: {name: $('#search').val()},
            success: function (data) {
                /* 模拟搜索数据 开始 */
              /*  var treeMn = [];
                for (var i in data.tree) {
                    if (data.tree[i].id === $('#search').val()) {
                        treeMn.push(data.tree[i])
                    } else {
                        for (var j = 0; j < data.tree[i].children.length; j++) {
                            if (data.tree[i].children[j].id === $('#search').val()) {
                                treeMn.push(data.tree[i].children[j]);
                                if (data.tree[i].children[j].children) {
                                    data.tree[i].children[j].spread = true;
                                }
                            }
                        }
                    }
                }*/
                /* 模拟搜索数据 结束 */
                if (data.code === 1) {
                    $('#demo').html('');
                    layui.use('tree', function () {
                        layui.tree({
                            elem: '#demo' //传入元素选择器
                            , skin: 'coral' //设定颜色
                            , nodes: data.tree //获取数据
                        });
                    });
                } else {
                    alert('无搜索结果')
                }
            },
            error: function () {
                alert('查询失败')
            }
        });
    }
});

